home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.02 Feb 88 / c postcard / PostCard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-13  |  7.5 KB  |  393 lines  |  [TEXT/KAHL]

  1. /* 
  2.  * PostCard, version 1.0, Lightspeed C
  3.  * demonstrates using data forks and resource tricks
  4.  *
  5.  * Copyright 1987 by Joel McNamara - All Rights Reserved
  6.  * for MacTutor Magazine
  7.  *
  8.  * initial coding - October 12, 1987
  9.  */
  10.   
  11. #include    "MacTypes.h"
  12. #include    "QuickDraw.h"
  13. #include    "WindowMgr.h"
  14. #include    "EventMgr.h"
  15. #include    "MenuMgr.h"
  16. #include    "FileMgr.h"
  17. #include    "MemoryMgr.h"
  18. #include    "ResourceMgr.h"
  19. #include    "StdFilePkg.h"
  20. #include    "OSUtil.h"
  21. #include    "DialogMgr.h"
  22.  
  23.  
  24. /* defines... */
  25. #define MessageDLOG    255
  26. #define    AboutDLOG    256
  27. #define    HelpDLOG    257
  28.  
  29. #define    AppleMENU    1
  30. #define    About            1
  31.  
  32. #define    FileMENU    255
  33. #define    Quit            1
  34.  
  35. #define    CommandMENU    256
  36. #define MakeCard        1
  37. #define Help            3
  38.  
  39.  
  40. /* globals... */
  41. Boolean            done;
  42. char            theString[256];
  43. MenuHandle        AppleMenuHndl, FileMenuHndl, CommandMenuHndl;
  44. EventRecord        theEvent;
  45. DialogPtr         theDialog;
  46. int             resRef,dummy,myError;
  47.  
  48.  
  49. /* main */
  50. main()
  51. {
  52.     Initialize();
  53.     do{
  54.         MainLoop();
  55.     }while(!done);
  56.  
  57.  
  58. /* as always... */
  59. Initialize()
  60. {
  61.     InitGraf(&thePort);
  62.     InitFonts();
  63.     InitWindows();
  64.     InitMenus();
  65.     TEInit();
  66.     InitDialogs(0);
  67.     InitCursor();
  68.     FlushEvents(everyEvent, 0);
  69.  
  70.     SetUpMenus();
  71.  
  72.     done = FALSE;
  73.     myError = 0;
  74. }
  75.  
  76.  
  77. /* make the menus */
  78. SetUpMenus()
  79. {
  80.     AppleMenuHndl = GetMenu(AppleMENU);
  81.     AddResMenu(AppleMenuHndl, 'DRVR');
  82.     InsertMenu(AppleMenuHndl, 0);
  83.  
  84.     FileMenuHndl = GetMenu(FileMENU);
  85.     InsertMenu(FileMenuHndl, 0);
  86.  
  87.     CommandMenuHndl = GetMenu(CommandMENU);
  88.     InsertMenu(CommandMenuHndl, 0);
  89.  
  90.     DrawMenuBar();
  91. }
  92.  
  93.  
  94. /* generic dialog displayer */
  95. DoDialog(theDLOG)
  96. int    theDLOG;
  97. {    
  98.     theDialog = GetNewDialog(theDLOG, 0, -1);
  99.     DoButton(theDialog);
  100.     ModalDialog(0, &dummy);
  101.     DisposDialog(theDialog);
  102. }
  103.  
  104.  
  105. /* message display routine */
  106. DoMessage(theMessage)
  107. Str255    theMessage;
  108. {
  109.     theDialog = GetNewDialog(MessageDLOG, 0, -1);    
  110.     ParamText(theMessage, "\p", "\p", "\p");
  111.     DoButton(theDialog);
  112.     ModalDialog(0, &dummy);
  113.     DisposDialog(theDialog);    
  114. }
  115.  
  116.  
  117. /* loop until we're done */
  118. MainLoop()
  119. {
  120.     SystemTask();
  121.     if (GetNextEvent(everyEvent,&theEvent)) {
  122.         switch (theEvent.what) {
  123.             case mouseDown:
  124.                 DoMouseDown();    
  125.                 break;
  126.                 
  127.             default:
  128.                 break;
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134. /* if we have a mouse down */
  135. DoMouseDown()
  136. {
  137. WindowPtr     whichWindow;
  138. int            thePart;
  139.  
  140.     thePart = FindWindow(theEvent.where,&whichWindow);
  141.     switch(thePart) {
  142.         case inMenuBar:
  143.             DoMenu();    
  144.             break;
  145.  
  146.         case inSysWindow:
  147.             SystemClick(&theEvent, whichWindow);
  148.             break;
  149.  
  150.         default:
  151.             break;
  152.     }
  153. }
  154.  
  155.  
  156. /* if a menu hit then... */
  157. DoMenu()
  158. {
  159. long menuChoice;
  160.  
  161.     menuChoice = MenuSelect(theEvent.where);
  162.     DoMenuItem(menuChoice);
  163. }
  164.  
  165.  
  166. /* which menu was selected? */
  167. DoMenuItem(menuChoice)
  168. long menuChoice;
  169. {
  170. int     theMenu,theItem;
  171.  
  172.     if (menuChoice != 0 ) {
  173.         theItem = menuChoice;
  174.         theMenu = (menuChoice >> 16);
  175.  
  176.         switch (theMenu) {
  177.             case AppleMENU:
  178.                 DoDA(theItem);
  179.                 break;
  180.                 
  181.             case FileMENU:
  182.                 done = TRUE;
  183.             break;
  184.             
  185.             case CommandMENU:
  186.                 DoCommands(theItem);
  187.         
  188.             default:
  189.                 break;
  190.         }
  191.         HiliteMenu(0);
  192.     }
  193. }
  194.  
  195.  
  196. /* the apple menu was hit */
  197. DoDA(theItem)
  198. int theItem;
  199. {
  200. int accNumber;
  201.  
  202.     if (theItem == About)
  203.         DoDialog(AboutDLOG);
  204.     else{
  205.         GetItem(AppleMenuHndl,theItem,theString);
  206.         accNumber = OpenDeskAcc(theString);
  207.     }
  208. }
  209.  
  210.  
  211. /* the commands menu was hit */
  212. DoCommands(theItem)
  213. int theItem;
  214. {
  215.     switch(theItem) {
  216.         case MakeCard:
  217.                 ConvertFile();
  218.             break;
  219.  
  220.         case Help:
  221.                 DoDialog(HelpDLOG);
  222.             break;
  223.             
  224.         default:
  225.             break;
  226.     }
  227. }
  228.  
  229.  
  230. /* make a postcard */
  231. ConvertFile()
  232. {
  233. char*        buffer;
  234. long        theEOF;
  235. int            currentResRef,refNum,destRef;
  236. Handle        codeHandle;
  237.  
  238. Point        myPoint;
  239. SFReply        paintReply, myReply;
  240. SFTypeList    myTypes;
  241. FInfo        myFInfo;
  242. OSErr        myErr;            
  243.  
  244.     myTypes[0] = 'PNTG';
  245.     myPoint.h = 90;
  246.     myPoint.v = 90;
  247.  
  248.     /* first we create the application */    
  249.     DoMessage("\pFirst name the PostCard you want to create.");
  250.     SFPutFile(myPoint, "\pEnter PostCard name:", "\pMy PostCard", 0,
  251.         &myReply);
  252.     if (myReply.good)
  253.     {
  254.         SetVol(0, myReply.vRefNum);
  255.         CreateResFile(myReply.fName);
  256.         
  257.         /* make it what we want... */
  258.         myFInfo.fdType = 'APPL';
  259.         myFInfo.fdCreator = 'PDIS';
  260.         myFInfo.fdFlags = fHasBundle;
  261.         myFInfo.fdLocation.h = 0;
  262.         myFInfo.fdLocation.v = 0;
  263.         SetFInfo(myReply.fName, myReply.vRefNum, &myFInfo);
  264.         
  265.         /* open its resource fork */
  266.         resRef = OpenResFile(myReply.fName);
  267.         if (resRef != -1)
  268.             {
  269.                 /* get PostCard's resource ref number and then
  270.                    start moving the resources */
  271.                 currentResRef = CurResFile();
  272.                 
  273.                 /* first the obvious ones */
  274.                 ResTransfer('BNDL',129, 'BNDL',128);
  275.                 ResTransfer('PDIS',0,'PDIS',0);
  276.                 ResTransfer('FREF',128,'FREF',128);
  277.                 ResTransfer('ICN#',129,'ICN#',128);
  278.                 
  279.                 /* now we grab the FAKEs and make them CODE */
  280.                 ResTransfer('FAKE',0,'CODE',0);
  281.                 ResTransfer('FAKE',1,'CODE',1);
  282.                 ResTransfer('FAKE',2,'CODE',2);
  283.  
  284.                 /* finally get the DUMYs and convert them */
  285.                 ResTransfer('DUMY',0,'CREL',2);
  286.                 ResTransfer('DUMY',1,'DATA',0);
  287.                 ResTransfer('DUMY',2,'DREL',0);
  288.                 ResTransfer('DUMY',3,'STRS',0);
  289.                 ResTransfer('DUMY',4,'ZERO',0);                
  290.  
  291.                 /* update the new application, close it, make PostCard
  292.                    the current open resource fork */
  293.                 UpdateResFile(resRef);
  294.                 CloseResFile(resRef);
  295.                 UseResFile(currentResRef);
  296.                 
  297.                 /* see if we have any errors - open the application */
  298.                 if ( (FSOpen(myReply.fName, myReply.vRefNum, &destRef) != noErr) ||
  299.                         (myError == 1) ) {
  300.                     DoMessage("\pSorry a resource error prevented the PostCard from being created."); 
  301.                     FSDelete(myReply.fName,myReply.vRefNum);
  302.                     myError = 0;                    
  303.                     }
  304.                 else
  305.                 {                                                                    
  306.                 /* here we select the paint file we want to convert */
  307.                 DoMessage("\pNow select the Paint file you want to display.");
  308.                 SFGetFile(myPoint, "\p", 0, 1, myTypes, 0, &paintReply);
  309.                 if (paintReply.good)
  310.                     {    
  311.                         /* open, read, and write into the document */
  312.                         if (FSOpen(paintReply.fName, paintReply.vRefNum, &refNum) == noErr)
  313.                             {
  314.                                 if ( GetEOF(refNum, &theEOF) != noErr)
  315.                                     myError = 1;
  316.                                 buffer = NewPtr(theEOF);
  317.                                 if ( FSRead(refNum, &theEOF, buffer) != noErr)
  318.                                     myError = 1;
  319.                                 if ( FSWrite(destRef, &theEOF, buffer) != noErr)
  320.                                     myError = 1;
  321.                                 myErr = FSClose(refNum);
  322.                                 myErr = FSClose(destRef);
  323.                                 if (myError != 1)
  324.                                     DoMessage("\pYour PostCard has been created!");
  325.                             }            
  326.                     }
  327.                     else
  328.                     {
  329.                         /* get rid of file we've created */
  330.                         FSDelete(myReply.fName,myReply.vRefNum);    
  331.                     }
  332.                 }
  333.                         
  334.         }
  335.         else
  336.             myError = 1;
  337.             
  338.     if (myError == 1) {
  339.         /* opps, problems - blitz the file */
  340.         DoMessage("\pSorry an error prevented the PostCard from being created."); 
  341.         FSDelete(myReply.fName,myReply.vRefNum);
  342.         }
  343.     }
  344. }
  345.  
  346.  
  347. /* grab a resource and rename it routine */
  348. ResTransfer(sourceRes, sourceID, destRes, destID)
  349. ResType    sourceRes, destRes;
  350. int        sourceID, destID; 
  351. {
  352. Handle    codeHandle;
  353.     codeHandle = NewHandle(0);
  354.     
  355.     /* get the source resource */
  356.     codeHandle = GetResource(sourceRes,sourceID);
  357.     
  358.     /* set the current res file to the stand alone document */
  359.     UseResFile(resRef);
  360.     
  361.     /* detach the source resource... */
  362.     DetachResource(codeHandle);
  363.     
  364.     /* and add it to the document */
  365.     AddResource(codeHandle, destRes, destID, "\p");
  366.     if (ResError() != noErr) {
  367.         DoMessage("\pSorry, couldn't copy the required resource.");
  368.         myError = 1;
  369.         }
  370. }
  371.  
  372.  
  373. /* fancy button for the modals */
  374. DoButton(myDialog)
  375. DialogPtr    myDialog;
  376. {
  377. short        myType;
  378. Handle        myHandle;
  379. Rect        myBox;
  380. GrafPtr        dummyPort;
  381.  
  382.     GetPort(&dummyPort);
  383.     SetPort(myDialog);
  384.     GetDItem( myDialog, 1, &myType, &myHandle, &myBox );
  385.     InsetRect(&myBox, -4, -4);
  386.     PenSize(3, 3);
  387.     FrameRoundRect(&myBox, 16, 16);
  388.     PenNormal();
  389.     SetPort(dummyPort);
  390. }
  391.  
  392.